<template>
  <div class="login-bg">
    <div class="login-container">
      <div class="login-header">
        <img class="logo mr10" src="../../assets/img/logo.svg" alt="" />
        <div class="login-title">登录</div>
      </div>
      <el-form :model="loginInfo" :rules="rules" ref="loginForm" size="large">
        <el-form-item prop="email">
          <el-input v-model="loginInfo.email" placeholder="邮箱">
            <template #prepend>
              <el-icon>
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="密码"
            v-model="loginInfo.password"
            @keyup.enter="submitForm(loginForm)"
          >
            <template #prepend>
              <el-icon>
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <div class="pwd-tips">
          <el-checkbox
            class="pwd-checkbox"
            v-model="checked"
            label="记住密码"
          />
          <el-link type="primary" @click="$router.push('/reset-pwd')"
            >忘记密码</el-link
          >
        </div>
        <el-button
          class="login-btn"
          type="primary"
          size="large"
          @click="submitForm(loginForm)"
          >登录</el-button
        >
        <p class="login-tips">Tips : 邮箱和密码随便填。</p>
        <p class="login-text">
          没有账号？<el-link type="primary" @click="$router.push('/register')"
            >立即注册</el-link
          >
        </p>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive } from "vue";
  import { useRouter } from "vue-router";
  import { ElMessage } from "element-plus";
  import type { FormInstance, FormRules } from "element-plus";

  import type { LoginInfo } from "@/types/user";
  import { apiUserLogin } from "@/api/user";

  const lgStr = localStorage.getItem("login-info");
  const defLoginInfo = lgStr ? JSON.parse(lgStr) : null;
  const checked = ref(lgStr ? true : false);

  const router = useRouter();

  const loginInfo = reactive<LoginInfo>({
    email: lgStr ? defLoginInfo.email : '',
    password: lgStr ? defLoginInfo.password : '',
  });

  const rules: FormRules = {
    email: [
      { required: true, message: "请输入邮箱", trigger: "blur" },
      {
        type: "email",
        message: "请输入有效的邮箱地址",
        trigger: ["blur", "change"],
      },
    ],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  };

  const loginForm = ref<FormInstance>();
  const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    formEl.validate(async (valid: boolean) => {
      if (valid) {
        try {
          const result = await apiUserLogin(loginInfo);
          if (result == true) {
            if (checked.value) {
              localStorage.setItem("login-info", JSON.stringify(loginInfo));
            } else {
              localStorage.removeItem("login-info");
            }
            router.back();
          }
        } catch (err) {
          alert(err);
        }
      } else {
        ElMessage.error("登录失败，格式错误");
        // return false;
      }
    });
  };
</script>

<style scoped>
  .login-bg {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100vh;
    background: url(../../assets/img/login-bg.jpg) center/cover no-repeat;
  }

  .login-header {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 40px;
  }

  .logo {
    width: 35px;
  }

  .login-title {
    font-size: 22px;
    color: #333;
    font-weight: bold;
  }

  .login-container {
    width: 450px;
    border-radius: 5px;
    background: #fff;
    padding: 40px 50px 50px;
    box-sizing: border-box;
  }

  .pwd-tips {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 14px;
    margin: -10px 0 10px;
    color: #787878;
  }

  .pwd-checkbox {
    height: auto;
  }

  .login-btn {
    display: block;
    width: 100%;
  }

  .login-tips {
    font-size: 12px;
    color: #999;
  }

  .login-text {
    display: flex;
    align-items: center;
    margin-top: 20px;
    font-size: 14px;
    color: #787878;
  }
</style>
